import 'dart:async';

import 'package:flutter/widgets.dart';
import 'package:ladder_cash/app/app.dart';
import 'package:ladder_cash/app/com/plugin/common_plugin.dart';
import 'package:ladder_cash/app/key_mix.dart';
import 'package:ladder_cash/app/page/home/home_page.dart';
import 'package:ladder_cash/app/page/login/safety_verify/safety_verify_view.dart';
import 'package:ladder_cash/generated/l10n.dart';
import 'package:get/get.dart';
import 'package:liuxlcommon/util/toast_util.dart';
import 'package:ladder_cash/app/com/http/http_request.dart';
import 'package:ladder_cash/app/com/http/Res.dart';
import 'package:ladder_cash/app/com/service/burial_point_service.dart';
import 'package:ladder_cash/app/com/service/user_service.dart';

class LoginLogic extends GetxController {
  String phone = "", password = "", rePassword = "", code = ""; //默认 202209
  var codeCount = 60.obs;
  UserService userService = UserService();
  bool agree = true;
  bool isBack = true;
  FocusNode codeFocusNode = FocusNode();

  //验证吗登录
  bool _isCodeLogin = false;

  get isCodeLogin => _isCodeLogin;

  @override
  onInit() {
    try {
      isBack = Get.arguments['isBack'];
    } catch (e) {}
  }

  get isVerPhone {
    return true;
    if (phone.length > 7 && code.length == 6) {
      return true;
    }
    return false;
  }

  set isCodeLogin(b) {
    _isCodeLogin = b;
    update();
  }

  Timer? _timer;

  login() async {
    if (!agree) {
      ToastUtils.toast(App.localeEn
          ? "Please read and agree to the Agreement"
          : "Please read and agree to the Agreement");
      return;
    }

    if (!isVerPhone) return;
    if (phone.length < 7) {
      ToastUtils.toast(App.localeEn
          ? "Please enter the correct mobile phone number！"
          : "Silakan masukkan nomor ponsel yang benar!");
      return null;
    }
    if (code.isEmpty) {
      ToastUtils.toast(App.localeEn
          ? "please enter verification code！"
          : "silakan masukkan kode verifikasi！");
      return null;
    }
    dynamic data;
    data =
        await userService.login(phone: phone, password: password, code: code);
    if (data == null) return null;
    BurialPointService.end(BPoint.register);
    CommonPlugin.initAdjust;
    phone = '';
    code = '';
    if (isBack) {
      Get.back();
    } else {
      Get.offAll(HomePage());
    }
  }

  getVoiceMessage() async {
    if (phone.length < 7) {
      ToastUtils.toast(App.localeEn
          ? "Please enter the correct mobile phone number！"
          : "Silakan masukkan nomor ponsel yang benar!");
      return null;
    }
    BurialPointService.start(BPoint.register);
    Res r = await HttpRequest().post('/service/login/send-voice-code'.hx,
        {'phone'.hx: phone, 'smsType'.hx: 1},
        loadingText: S.current.loading, parse: false);
    ToastUtils.toast(r.msg);
  }

  getCode() async {
    if (phone.length < 7) {
      ToastUtils.toast(App.localeEn
          ? "Please enter the correct mobile phone number！"
          : "Silakan masukkan nomor ponsel yang benar!");
      return null;
    }

    // if (await safetyVerity(phone.trim())) return;

    if (codeCount.value == 60) {
      BurialPointService.start(BPoint.register);
      Res r = await HttpRequest().post('/service/login/send-sms-code-new'.hx,
          {'phone'.hx: phone, 'smsType'.hx: 1},
          loadingText: S.current.loading, parse: false);
      if (r.success) {
        codeCount--;
        _timer = Timer.periodic(const Duration(milliseconds: 1000), (timer) {
          if (codeCount.value == 1) {
            codeCount.value = 60;
            _timer?.cancel();
            return;
          }
          codeCount--;
        });
        codeFocusNode.requestFocus();
      } else {}
      ToastUtils.toast(r.msg);
    }
  }

  // Future<bool> safetyVerity(phone) async {
  //   Res s = await HttpRequest().postRes(
  //       '/service/login/if-check'.hx, {'username'.hx: phone},
  //       loadingText: S.current.loading);
  //   if (s.data?["check_auth"] == 1) {
  //     await Get.to(const SafetyVerifyPage(), arguments: {"phone": phone});
  //     return true;
  //   }
  //   return false;
  // }

  @override
  void onClose() {
    _timer?.cancel();
    super.onClose();
  }
}
